package com.hjm.app.activity;

import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hjm.app.R;
import com.hjm.java.entity.GoodsListVO;
import com.hjm.java.entity.Store;
import com.hjm.app.fragment.store.FragmentStoreAllGoods;
import com.hjm.app.fragment.store.FragmentStoreDetail;
import com.hjm.app.fragment.store.FragmentStoreHome;
import com.hjm.app.tool.ImageLabelSelectStateObserve;
import com.hjm.ui.comment.CommentFragment;
import com.hjm.ui.goods.adapter.GoodsItemAdapter;

import java.util.ArrayList;
import java.util.List;

public class StoreActivity extends BaseActivity implements
        View.OnClickListener,
        GoodsItemAdapter.Listener,
        FragmentStoreHome.CallBack{

    private static final String TAG = "StoreActivity";
    private LinearLayout layoutStoreHome, layoutStoreAllGoods, layoutPinglun, layoutStoreDetail;
    private ImageView storeImg, storeBg;
    private ImageLabelSelectStateObserve imageLabelSelectStateObserve;
    private ImageButton backImageButton;

    private Store store;

    private int storeId = 0;
    private boolean isWebViewPageFinished = false;

    @Override
    protected void onResume() {
        super.onResume();
        //Log.d(TAG, "onResume: 刷新 WebView");
        //((FragmentStoreHome)mFragmentList.get(0)).reloadWebViewStoreHome();
    }

    @Override
    void initData() {
        // 获取传递过来的值
        Intent intent = getIntent();
        if (intent != null) {
            this.storeId = intent.getIntExtra("storeId",0);
        }
        mFragmentList = new ArrayList<>();
        // 添加商铺首页片段
        mFragmentList.add(new FragmentStoreHome(this));
        // 添加全部商品片段
        mFragmentList.add(new FragmentStoreAllGoods());
        // 添加评论片段
        initStoreComment();
        // 添加店铺详情片段
        mFragmentList.add(new FragmentStoreDetail());

        // 默认显示店铺首页
        switchFragmentPosition(0);

        getStoreData(storeId);
        getAllGoodsItemData(storeId);
    }

    // 初始化评论片段
    private void initStoreComment() {
        storeService.initStoreComment(storeId,(isSuccess, t, message) -> {
            if (isSuccess) {
                Log.d(TAG, "getCommentList: 获取成功 "+t.size());
                CommentFragment commentFragment = CommentFragment.newInstance(t);
                commentFragment.setOnClickListener(StoreActivity.this);
                // 隐藏按钮
                commentFragment.hideAddButton();
                mFragmentList.add(commentFragment);
            } else {
                Log.e(TAG, "getCommentList: 获取失败 "+message);
            }
        });
    }

    @Override
    int getView() {
        return R.layout.activity_store;
    }

    @Override
    void initView() {
        layoutStoreHome = findViewById(R.id.layout_store_home);
        layoutStoreAllGoods = findViewById(R.id.layout_store_all_goods);
        layoutPinglun = findViewById(R.id.layout_store_pinglun);
        layoutStoreDetail = findViewById(R.id.layout_store_detail);

        storeImg = findViewById(R.id.image_view_store_img);
        storeBg = findViewById(R.id.image_view_store_bg);

        imageLabelSelectStateObserve = new ImageLabelSelectStateObserve(new ImageLabelSelectStateObserve.UpdateUI() {
            @Override
            public void updateUIToActive(View viewSub) {
                if (viewSub instanceof ImageView) {
                    ImageView imageView = (ImageView) viewSub;
                    // 对imageView进行操作
                    imageView.setColorFilter(Color.parseColor("#FFA500")); // 橙色tint
                } else if (viewSub instanceof TextView) {
                    TextView textView = (TextView) viewSub;
                    // 对textView进行操作
                    textView.setTextColor(Color.parseColor("#FFA500")); // 橙色文字
                }
            }

            @Override
            public void updateUIToNonactive(View viewSub) {
                if (viewSub instanceof ImageView) {
                    ImageView imageView = (ImageView) viewSub;
                    // 对imageView进行操作
                    imageView.setColorFilter(null); // 清除tint，恢复默认图片颜色
                } else if (viewSub instanceof TextView) {
                    TextView textView = (TextView) viewSub;
                    // 对textView进行操作
                    textView.setTextColor(Color.parseColor("#DD000000")); // 恢复默认文字颜色
                }
            }

            @Override
            public void onClick(View view) {
                if(view.getId() == R.id.layout_store_home){
                    switchFragmentPosition(0);
                } else if(view.getId() == R.id.layout_store_all_goods){
                    switchFragmentPosition(1);
                } else if(view.getId() == R.id.layout_store_detail){
                    switchFragmentPosition(2);
                } else if (view.getId() == R.id.layout_store_pinglun){
                    if (mFragmentList.size()==4)
                        switchFragmentPosition(3);
                }
            }

            @Override
            public void updateUIToActiveWithExtraView(ImageLabelSelectStateObserve.ViewParent viewParent) {
            }

            @Override
            public void updateUIToNonactiveWithExtraView(ImageLabelSelectStateObserve.ViewParent viewParent) {
            }
        });
        imageLabelSelectStateObserve.addToObserveGroup(layoutStoreHome, true);
        imageLabelSelectStateObserve.addToObserveGroup(layoutStoreAllGoods, false);
        imageLabelSelectStateObserve.addToObserveGroup(layoutPinglun, false);
        imageLabelSelectStateObserve.addToObserveGroup(layoutStoreDetail, false);

        backImageButton = findViewById(R.id.imageButton_back);
        backImageButton.setOnClickListener((v)->{
            finish();
        });
    }

    private void getStoreData(int storeId){
        storeService.getStoreById(storeId,
                store->{
                    Log.d(TAG, "onSuccess: \n"+store.toString());
                    setStoreData(store);
                },
                msg -> {
                    Log.e(TAG, "onFailure: 获取失败 "+msg);
                });
    }
    private void setStoreData(Store store){
        this.store = store;

        FragmentStoreDetail fragmentStoreDetail = (FragmentStoreDetail)mFragmentList.get(2);
        FragmentStoreHome fragmentStoreHome = (FragmentStoreHome)mFragmentList.get(0);
        if (isWebViewPageFinished)
            fragmentStoreHome.setHomeContent(store.getHome());
        // 设置店铺头像
        Glide.with(this).load(storeService.baseImgUrl+store.getIcon()).into(storeImg);
        // 设置店铺背景
        Glide.with(this).load(storeService.baseImgUrl+(store.getBg())[0]).into(storeBg);
        fragmentStoreDetail.setDetail(store.getInfo());
    }
    public void getAllGoodsItemData(int storeId){
        storeService.getStoreAllGoods(storeId, ()->{
            List<GoodsListVO> goodsListVOList = storeService.getGoodsListVOList();
            setGoodsItemList(goodsListVOList);
        });
    }

    public void setGoodsItemList(List<GoodsListVO> goodsListVOList) {
        FragmentStoreAllGoods fragmentStoreAllGoods = (FragmentStoreAllGoods)mFragmentList.get(1);
        fragmentStoreAllGoods.setGoodsItemList(goodsListVOList, this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == com.hjm.ui.R.id.comment_add_button){
            Log.d(TAG, "initStoreComment: 点击了新增按钮");
            // 跳转到评论页面
            Intent intent = new Intent(StoreActivity.this, CommentActivity.class);
            intent.putExtra("storeId",storeId);
            intent.putExtra("isNewComment", true);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onClick(int id) {
        Log.d(TAG, "onClick:点击了商品 "+id);
        Intent intent = new Intent(StoreActivity.this, GoodsDetailActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); // 移到栈顶，不会创建新的GoodsDetailActivity实例
        intent.putExtra("goodsId",id);
        startActivity(intent);
    }

    @Override
    public void onPageFinished() {
        Log.d(TAG, "onPageFinished: WebView 加载完成");
        if (mFragmentList!=null&&mFragmentList.size()>0&&store!=null) {
            ((FragmentStoreHome) mFragmentList.get(0)).setHomeContent(store.getHome());
        } else {
            isWebViewPageFinished = true;
        }
    }
}